package com.zking.frame;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.List;

import javax.swing.*;
import javax.swing.border.MatteBorder;

import com.zking.dao.UserDao;
import com.zking.entity.User;
import com.zking.utils.Tool工具;

/*注册页面*/
public class Register {

	private JLabel textJLabel, userJLabel, passwordJLabel, teJLabel;// 书写标题，书写用户提示，书写密码提示，注册成功提示.
	private JTextField userField, nameField;
	private JPasswordField passwordField;// 文本框
	private JButton landButton, registerButton;// 登陆注册按钮
	private JPanel jPanel;
	private boolean ab = false, bc = false, cd = false;
	private List<User> usList;

	public Register(JFrame a) {
		a.dispose();
		JFrame jFrame = Tool工具.getJFrame();
		jFrame.getContentPane().setBackground(Color.white);

		//获取用户表信息
		UserDao userDao = new UserDao();
		usList = userDao.queryStudents();

		jPanel = new JPanel();
		jPanel.setBounds(20, 50, 600, 900);
		jPanel.setLayout(null);
		jPanel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(63, 247, 18)));
		jPanel.setBackground(new Color(133, 250, 104));
		jFrame.add(jPanel, 0);

		// 加入背景
		ImageIcon icon = new ImageIcon(Land.class.getResource("注册背景.jpg"));
		JLabel jLabel = new JLabel(icon);
		jLabel.setBounds(0, 0, 600, 900);
		jPanel.add(jLabel, -1);

		// 书写标题
		textJLabel = new JLabel("注册账号");
		textJLabel.setForeground(new Color(18, 220, 214));
		textJLabel.setFont(new Font("华文琥珀", Font.PLAIN, 40));
		textJLabel.setBounds(200, 170, 400, 100);
		jPanel.add(textJLabel, 0);

		// 书写提示
		userJLabel = new JLabel("用户名：");
		userJLabel.setBounds(130, 350, 150, 50);
		userJLabel.setFont(new Font("黑体", Font.BOLD, 25));
		jPanel.add(userJLabel, 0);
		passwordJLabel = new JLabel("密码：");
		passwordJLabel.setBounds(130, 450, 100, 50);
		passwordJLabel.setFont(new Font("黑体", Font.BOLD, 25));
		jPanel.add(passwordJLabel, 0);

		// 书写用户框和密码框
		userField = new JTextField("请设置你的用户名");
		userField.setBounds(230, 350, 200, 50);
		userField.setForeground(Color.gray);
		userField.setFont(new Font("黑体", Font.BOLD, 20));
		jPanel.add(userField, 0);

		// 设置聚焦失焦事件
		userField.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				if (userField.getText().equals("")) {
					userField.setText("请设置你的用户名");
					userField.setForeground(Color.gray);
					ab = false;
				} else {
					if (!userField.getText().matches("[0-9a-zA-Z]{6,12}")) {
						userField.setText("6-12（数字或字母）");
						return;
					}
					for (int i = 0; i < usList.size(); i++) {
						if (userField.getText().equals(usList.get(i).getUserName())) {
							userField.setText("你设置的用户名已存在，请重新输入");
							passwordField.setText("");
							ab = false;
							return;
						}else {
							ab = true;
						}
					}

				}
			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				if (userField.getText().equals("请设置你的用户名") || userField.getText().equals("你设置的用户名已存在，请重新输入")
						|| userField.getText().equals("6-12（数字或字母）")) {
					userField.setText("");
					userField.setForeground(Color.black);
				}

			}
		});

		passwordField = new JPasswordField();
		passwordField.setBounds(230, 450, 200, 50);
		passwordField.setFont(new Font("黑体", Font.BOLD, 20));
		passwordField.setForeground(Color.gray);
		jPanel.add(passwordField, 0);

		JLabel jLabel1 = new JLabel("",0);
		jLabel1.setBounds(230, 500, 200, 50);
		jLabel1.setFont(new Font("黑体", Font.BOLD, 20));
		jPanel.add(jLabel1, 0);
		// 设置聚焦失焦事件
		passwordField.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				if (passwordField.getText().equals("")) {
					jLabel1.setText("请设置你的密码");
					jLabel1.setForeground(Color.gray);
					bc = false;
				} else {
					if (!passwordField.getText().matches("[0-9a-zA-Z]{6,12}")) {
						jLabel1.setText("6-12(数字或字母)");
						return;
					}else {
						bc = true;
					}
				}
			}
			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				jLabel1.setText("6-12(数字或字母)");
				jLabel1.setForeground(Color.black);
			}
		});

		// 设置文本标签和文本框存储名字
		JLabel namejLabel = new JLabel("手机号码：");
		namejLabel.setBounds(110, 550, 150, 50);
		namejLabel.setFont(new Font("黑体", Font.BOLD, 25));
		jPanel.add(namejLabel, 0);
		nameField = new JTextField("用于找密码");
		nameField.setBounds(230, 550, 200, 50);
		nameField.setForeground(Color.gray);
		nameField.setFont(new Font("黑体", Font.BOLD, 20));
		jPanel.add(nameField, 0);

		// 书写注册成功提示
		teJLabel = new JLabel("", 0);
		teJLabel.setBounds(100, 710, 350, 50);
		teJLabel.setFont(new Font("黑体", Font.BOLD, 20));
		teJLabel.setForeground(new Color(3, 147, 42));
		jPanel.add(teJLabel, 0);

		// 设置聚焦失焦事件
		nameField.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				if (nameField.getText().equals("")) {
					nameField.setText("用于找密码");
					nameField.setForeground(Color.gray);
					cd = false;
				} else {
					if (!nameField.getText().matches("^1[3|4|5|7|8][0-9]\\d{4,8}$")) {
						nameField.setText("请输入正确的号码");
					}else {
						cd = true;
					}
				}
			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				if (nameField.getText().equals("用于找密码") || nameField.getText().equals("请输入正确的号码")) {
					nameField.setText("");
					nameField.setForeground(Color.black);
				}

			}
		});
		// 设置按钮
		registerButton = new JButton("返回登陆");
		registerButton.setBounds(130, 650, 110, 50);
		registerButton.setFont(new Font("黑体", Font.BOLD, 15));
		registerButton.setBackground(new Color(203, 232, 234));
		registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
		// 设置点击事件
		registerButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new Land(jFrame);
			}
		});
		jPanel.add(registerButton, 0);

		// 注册按钮
		landButton = new JButton("注册");
		landButton.setBounds(240, 650, 190, 50);
		landButton.setFont(new Font("黑体", Font.BOLD, 15));
		landButton.setBackground(new Color(203, 232, 234));
		landButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
		jPanel.add(landButton, 0);

		// 书写点击事件
		landButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (landButton.getText().equals("注册")) {
					if (ab && bc && cd) {
						User user = new User();
						user.setUserName(userField.getText());
						System.out.println(userField.getText());
						user.setPassWord(passwordField.getText());
						System.out.println(passwordField.getText());
						user.setPhoneNumber(nameField.getText());
						System.out.println(nameField.getText());
						user.setSort(1+"");
						userDao.insertStudent(user);
						JOptionPane.showMessageDialog(jFrame,"注册成功");
						new Land(jFrame);
						usList = userDao.queryStudents();
					}
				}
			}
		});
		// 加载并刷新面板
		jFrame.repaint();
		jFrame.revalidate();
	}

}
